package com.goktech.controller;

import com.goktech.entity.Book;
import com.goktech.entity.User;
import com.goktech.entity.borrow;
import com.goktech.entity.collect;
import com.goktech.service.BookService;
import com.goktech.service.BorrowService;
import com.goktech.service.CollectService;
import com.goktech.service.UserService;
import com.goktech.service.imp.borrowServiceImp;
import com.goktech.service.imp.collectServiceImp;
import com.goktech.service.imp.userServiceImp;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName userLibraryController
 * @Description TODO
 * @Author 邱华英
 * @Date 2022/10/9 18:53
 * @Version 1.0
 */

@Controller
@RequestMapping("/user/library")
public class userLibraryController {

    @Autowired
    private BookService bookService;

    @Autowired
    private Book book;

    @Autowired
    private UserService userService;

    @Autowired
    private User user;

    @Autowired
    private collect collect;

    @Autowired
    private CollectService collectService;

    @Autowired
    private borrow b;

    @Autowired
    private BorrowService borrowService;

    @RequestMapping("/")
    public String bookSelectAll(){
        return "redirect:search/add?yeshu=0";
    }

    @RequestMapping("/search/add")
    public String searchAdd(Integer yeshu, HttpServletRequest request){
        List<Map<String, Object>> byAll = null;
        byAll = bookService.findByAll(yeshu * 5, 5);

        if(byAll.size() == 0 && yeshu!=0){
            byAll = bookService.findByAll((yeshu-1)*5,5);
        }else{
            yeshu=yeshu+1;
        }
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("books",byAll);
        System.out.println("yeshu:"+yeshu);
        return "user/library";
    }

    @RequestMapping("/search/delete")
    public String searchDelete(Integer yeshu,HttpServletRequest request){
        List<Map<String, Object>> byAll = null;
        if(yeshu>1){
            yeshu=yeshu-1;
        }
        byAll = bookService.findByAll((yeshu-1)*5,5);
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("books",byAll);
        return "user/library";
    }

    @PostMapping("/select")
    public String select(@Param("select")String select,@Param("text")String text,HttpServletRequest request) throws UnsupportedEncodingException {
        String text1 = new String(text.getBytes("ISO-8859-1"), "utf-8");
        List<Map<String, Object>> allList = null;
        if("bookName".equals(select)){
            book.setBName("%"+text1+"%");
            allList = bookService.findLikeBookName(book);
        }else if("author".equals(select)){
            book.setBAuthor("%"+text1+"%");
            allList = bookService.findLikeAuthor(book);
        }
        request.setAttribute("books",allList);
        request.setAttribute("yeshu",1);
        return "user/library";
    }

    @PostMapping("/collect")
    @ResponseBody
    public boolean collect(@Param("bookid")Integer bookid,HttpServletRequest request){
        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");

        user.setZhanghao(zhanghao);
        User byUser = userService.findByUser(user);

        collect.setUserId(byUser.getId());
        collect.setBookId(bookid);

        collect byCollectOne = collectService.findByCollectOne(collect);

        if(byCollectOne!= null){
            return false;
        }else{
            collect.setCollectTime(new Date());
            int i = collectService.insertCollect(collect);

            if(i > 0){
                return true;
            }else{
                return false;
            }
        }
    }

    @RequestMapping("/borrow")
    @ResponseBody
    public boolean borrow(@Param("bookid")Integer bookid,@Param("returnTime")String returnTime,HttpServletRequest request){
        book.setId(bookid);
        Book byBook = bookService.findByBook(book);
        int yuliang = byBook.getYuliang();
        if(yuliang <=0){
            return false;
        }else {
            HttpSession session = request.getSession();
            String zhanghao = (String) session.getAttribute("zhanghao");
            user.setZhanghao(zhanghao);
            User byUser = userService.findByUser(user);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date d =new Date();
            String format = simpleDateFormat.format(d);
            String[] split1 = format.split("-");
            String[] split2 = returnTime.split("-");
            boolean flag = false;
            for(int i = 0;i < split1.length;i++){
                int i1 = Integer.parseInt(split1[i]) ;
                int i2 = Integer.parseInt(split2[i]);
                if(i2 > i1){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                return false;
            }else{
                b.setBookId(byBook.getId());
                b.setUserId(byUser.getId());
                try {
                    Date returnTime1 = simpleDateFormat.parse(returnTime);
                    b.setReturnTime(returnTime1);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                b.setBorrowTime(new Date());
                int i1 = borrowService.insertBorrow(b);
                if(i1 > 0){
                    byBook.setYuliang(byBook.getYuliang()-1);
                    int i = bookService.updateBook(byBook);
                    if(i>0){
                       return true;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }

        }
    }


}
